package cn.edu.sdjzu.cs.dingcan.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShoppingCart implements Serializable {
    
    /**
     *
     */
    private static final long serialVersionUID = 371335366265940603L;
    private Map<Food, Integer> items;

    public ShoppingCart() {
        this.items = new HashMap<>();
    }

    /**
     * 向购物车添加菜品
     * @param food  菜品
     * @param count 数量
     */
    public void addItem(Food food, int count) {
        if(items.containsKey(food)) {
            this.items.replace(food, count);
        } else {
            this.items.put(food, count);
        }
    }

    /**
     * 在购物车中删除菜品
     * @param food  要删除的菜品
     */
    public void removeItem(Food food) {
        this.items.remove(food);
    }

    /**
     * 计算购物车中菜品总价格
     * @return
     */
    public BigDecimal calculateTotalPrice() {
        BigDecimal totalPrice = new BigDecimal(0);
        for(Map.Entry<Food,Integer> entry : items.entrySet()) {
            totalPrice = totalPrice.add(entry.getKey().getPrice().multiply(new BigDecimal(entry.getValue())));
        }
        return totalPrice;
    }

    public List<OrderItem> convertToOrderItems() {
        List<OrderItem> orderItems = new ArrayList<>();
        this.items.forEach((f, c) -> {
            OrderItem item = new OrderItem(f, c);
            orderItems.add(item);
        });
        return orderItems;
    }

    public Map<Food, Integer> getItems() {
        return this.items;
    }
}
